//
//  ApplyShopView.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/6/3.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "ApplyShopView.h"
#import <Masonry/Masonry.h>

@interface _FormUintView : UIView
@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UITextField * textfield;
@property (nonatomic,strong) UIView * btmLine;
- (void)loadTitle:(NSString *)title placeHolder:(NSString *)placeHolder keyboardType:(UIKeyboardType)type;
@end

@interface _CardUnitView : UIView
@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UIButton * cardFront;
@property (nonatomic,strong) UIButton * cardBack;
@property (nonatomic,copy) void (^didClickedCard)(NSInteger tag);

@end


@interface ApplyShopView()<UIScrollViewDelegate>{
    NSArray * _titles;
    NSArray * _placeHolders;
    NSArray * _keybordTypes;
}

@property (nonatomic,strong) UIView * contentView;
@property (nonatomic,strong) NSMutableArray<_FormUintView *> * units;
@property (nonatomic,strong) UIButton * submitBtn;
@property (nonatomic,strong) _CardUnitView * cardView;
@property (nonatomic,strong) UIButton  * redioBtn;
@property (nonatomic,strong) UILabel * xieyiLabel;

@end

@implementation ApplyShopView

- (instancetype)init{
    if (self = [super init]) {
        self.alwaysBounceVertical = YES;
        self.units = [NSMutableArray array];
        _titles = @[@"姓       名",@"身份证号",@"通讯地址"];
        _placeHolders = @[@"请输入姓名",@"请输入身份证号",@"请输入通讯地址"];
        _keybordTypes = @[@(UIKeyboardTypeDefault),@(UIKeyboardTypeNumberPad),@(UIKeyboardTypeDefault)];
        self.contentView = [UIView new];
        [self addSubview:self.contentView];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
            make.width.equalTo(@(ScreenWidth));
        }];
        self.delegate = self;
        [self _loadUI];
    }
    return self;
}
- (void)_loadUI{
    [_titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        _FormUintView * unitView = [[_FormUintView alloc] init];
        [unitView loadTitle:obj placeHolder:_placeHolders[idx] keyboardType:[_keybordTypes[idx] integerValue]];
        [self.units addObject:unitView];
        [self.contentView addSubview:unitView];
    }];
    [self.units enumerateObjectsUsingBlock:^(_FormUintView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.height.equalTo(@50);
            if (idx == 0) {
                make.top.equalTo(self.contentView).offset(20);
            }else{
                make.top.equalTo(self.units[idx - 1].mas_bottom);
            }
        }];
    }];
    [self.contentView addSubview:self.cardView];
    [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.units[self.units.count - 1].mas_bottom).offset(5);
    }];
    
    [self.contentView addSubview:self.redioBtn];
    [self.redioBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cardView.mas_bottom).offset(20);
        make.left.equalTo(self.contentView).offset(30);
        make.width.equalTo(@20);
        make.height.equalTo(@20);
    }];
    [self.contentView addSubview:self.xieyiLabel];
    [self.xieyiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.redioBtn.mas_right).offset(15);
        make.centerY.equalTo(self.redioBtn);
    }];
    
    [self.contentView addSubview:self.submitBtn];
    [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
        make.bottom.equalTo(self);
        make.height.equalTo(@35);
        make.top.equalTo(self.redioBtn.mas_bottom).offset(40);
    }];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self endEditing:YES];
}

- (void)updateFrontImg:(UIImage *)img{
    [self.cardView.cardFront setImage:img forState:UIControlStateNormal];
}

- (void)updateBackImg:(UIImage *)img{
    [self.cardView.cardBack setImage:img forState:UIControlStateNormal];
}

- (void)btnClicked:(UIButton *)btn{
    if ([btn isKindOfClass:[UIGestureRecognizer class]]) {
        if (self.didClickedIndex) {
            self.didClickedIndex(3);
        }
        return;
    }
    if (btn.tag == 2) {
        btn.selected = !btn.selected;
    }
    if (btn.tag > 2) {
        if (!self.redioBtn.isSelected) {
            [MBProgressHUD showError:self title:@"请同意店铺协议" code:@"" animated:YES];
            return;
        }
    }
    if (btn.tag == 4) { //点击提交
        if (self.units[0].textfield.text.length == 0) {
            [MBProgressHUD showError:self title:@"请输入姓名" code:@"" animated:YES];
            return;
        }
        if (self.units[1].textfield.text.length == 0) {
            [MBProgressHUD showError:self title:@"请输入身份证号" code:@"" animated:YES];
            return;
        }
        if (self.units[2].textfield.text.length == 0) {
            [MBProgressHUD showError:self title:@"请输入通讯地址" code:@"" animated:YES];
            return;
        }
    }
    if (self.didClickedIndex) {
        self.didClickedIndex(btn.tag);
    }
}

- (UIButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_submitBtn setBackgroundColor: [UIColor colorWithHex:0xd62e34 alpha:1]];
        [_submitBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_submitBtn setTitle:@"提交申请" forState:UIControlStateNormal];
        [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _submitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _submitBtn.tag = 4;
    }
    return _submitBtn;
}

- (_CardUnitView *)cardView{
    if (!_cardView) {
        _cardView = [[_CardUnitView alloc] init];
        __weak typeof(self) weakSelf = self;
        _cardView.didClickedCard = ^(NSInteger tag) {
            if (weakSelf.didClickedIndex) {
                weakSelf.didClickedIndex(tag);
            }
        };
    }
    return _cardView;
}

- (UIButton *)redioBtn{
    if (!_redioBtn) {
        _redioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_redioBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _redioBtn.selected = YES;
        [_redioBtn setImage:[UIImage imageNamed:@"er_notselected"] forState:UIControlStateNormal];
        [_redioBtn setImage:[UIImage imageNamed:@"er_selected"] forState:UIControlStateSelected];
        _redioBtn.tag = 2;
    }
    return _redioBtn;
}

- (UILabel *)xieyiLabel{
    if (!_xieyiLabel) {
        _xieyiLabel = [UILabel new];
//        _xieyiLabel.text = @"我已阅读《店铺协议》";
        NSMutableAttributedString * attr = [[NSMutableAttributedString alloc] initWithString:@"我已阅读《店铺协议》"];
        [attr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(4, 6)];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xd62e34 alpha:1] range:NSMakeRange(4, 6)];
        _xieyiLabel.attributedText = attr;
        UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnClicked:)];
        [_xieyiLabel addGestureRecognizer:gesture];
        _xieyiLabel.userInteractionEnabled = YES;
        _xieyiLabel.tag = 3;

    }
    return _xieyiLabel;
}

- (NSString *)name{
    return self.units[0].textfield.text;
}
- (NSString *)cardNum{
    return self.units[1].textfield.text;
}

- (NSString *)address{
    return self.units[2].textfield.text;
}


@end

///////////////////////////////////
///////////////////////////////////

@implementation _FormUintView

- (instancetype)init{
    if (self = [super init]) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.textfield];
        [self addSubview:self.btmLine];
        [self _layout];
    }
    return self;
}

- (void)loadTitle:(NSString *)title placeHolder:(NSString *)placeHolder keyboardType:(UIKeyboardType)type{
    self.titleLabel.text = title;
    self.textfield.placeholder  = placeHolder;
    self.textfield.keyboardType = type;
}

- (void)_layout{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.height.equalTo(@55);
    }];
    [self.textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.left.equalTo(self.titleLabel.mas_right).offset(30);
        make.right.equalTo(self).offset(-30);
    }];
    [self.btmLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-0.5);
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.height.equalTo(@.5);
    }];
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _titleLabel;
}

- (UITextField *)textfield{
    if (!_textfield) {
        _textfield = [[UITextField alloc] init];
        _textfield.font = [UIFont systemFontOfSize:16];
    }
    return _textfield;
}

- (UIView *)btmLine{
    if (!_btmLine) {
        _btmLine = [UIView new];
        _btmLine.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5];
    }
    return _btmLine;
}

@end

@implementation _CardUnitView

- (instancetype)init{
    if (self = [super init]) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.cardBack];
        [self addSubview:self.cardFront];
        [self _layout];
    }
    return self;
}

- (void)_layout{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self).offset(5);
    }];
    [self.cardFront mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
        make.right.equalTo(self.mas_centerX).offset(-7.5);
        make.height.equalTo(@(100));
        make.bottom.equalTo(self);
    }];
    [self.cardBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
        make.left.equalTo(self.mas_centerX).offset(7.5);
        make.height.equalTo(@(100));
    }];
}

- (void)btnClicked:(UIButton *)btn{
    if (self.didClickedCard) {
        self.didClickedCard(btn.tag);
    }
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        _titleLabel.text = @"身份证照";
    }
    return _titleLabel;
}

- (UIButton *)cardFront{
    if (!_cardFront) {
        _cardFront = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cardFront setBackgroundColor:[UIColor colorWithHex:0xfff3f2 alpha:1]];
        [_cardFront setImage:[UIImage imageNamed:@"er_cardfront"] forState:UIControlStateNormal];
        _cardFront.tag = 0;
         [_cardFront addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cardFront;
}

- (UIButton *)cardBack{
    if (!_cardBack) {
        _cardBack = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cardBack setBackgroundColor:[UIColor colorWithHex:0xfff3f2 alpha:1]];
        [_cardBack setTitle:@"身份证反面" forState:UIControlStateNormal];
        [_cardBack setImage:[UIImage imageNamed:@"er_cardback"] forState:UIControlStateNormal];
        _cardBack.tag = 1;
        [_cardBack addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cardBack;
}

@end
